package org.example.poiwebtest.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.example.poiwebtest.PhoneExcelverifiyName;
import org.example.poiwebtest.entity.PhoneDTO;
import org.example.poiwebtest.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/phone")
public class PhoneController {
    /**
     * 导出1  使用网上excel导出导入工具类  无需校验的导入导出
     *
     * @param response
     */
    @RequestMapping(value = "/export")
    public void exportExcel(HttpServletResponse response) throws IOException {

        List<PhoneDTO> phoneDTOList = new ArrayList<>();
        phoneDTOList.add(new PhoneDTO(6, "小米", new BigDecimal(33.00), LocalDateTime.now()));
        phoneDTOList.add(new PhoneDTO(2, "小米", new BigDecimal(33.00), LocalDateTime.now()));
        phoneDTOList.add(new PhoneDTO(18, "小米米米米米米", new BigDecimal(33.00), LocalDateTime.now()));
        phoneDTOList.add(new PhoneDTO(6, null, new BigDecimal(-1.02), LocalDateTime.now()));
        phoneDTOList.add(new PhoneDTO(6, "小米", new BigDecimal(3322.00), LocalDateTime.now()));
        phoneDTOList.add(new PhoneDTO(6, "小米", new BigDecimal(33.0067), LocalDateTime.now()));

        ExcelUtils.exportExcel(phoneDTOList, "手机信息表", "手机信息", PhoneDTO.class, "手机信息", response);
    }


    @Autowired
    private PhoneExcelverifiyName phoneExcelverifiyName;

    /**
     * 文件校验导入
     *
     * @return
     */
    @RequestMapping(value = "/import", headers = "content-type=multipart/form-data")
    public Object upload(MultipartFile file, HttpServletResponse resp) throws Exception {
        //导入的基本配置
        ImportParams params = new ImportParams();
        //表头一行
        params.setHeadRows(1);
        //标题一行
        params.setTitleRows(1);
        //代表导入这里是需要验证的（根据字段上的注解校验）
        params.setNeedVerify(true);
        //设及一个自定义校验 （自定义校验名字不可重复）
        params.setVerifyHandler(phoneExcelverifiyName);
        //使用框架自身导入工具
        ExcelImportResult<PhoneDTO> result = ExcelImportUtil.importExcelMore(file.getInputStream(), PhoneDTO.class, params);
        //导入成功的数据
        List<PhoneDTO> list = result.getList();
        //失败结果集
        List<PhoneDTO> failList = result.getFailList();
        //拿到导出失败的工作簿
        Workbook failWorkbook = result.getFailWorkbook();


        //验证是否有失败的数据
        if (result.isVerifyFail()) {

            ServletOutputStream fos = resp.getOutputStream();
            //mime类型
            resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            resp.setHeader("Content-disposition", "attachment;filename=error.xlsx");
            result.getFailWorkbook().write(fos);
            fos.close();
        }
        return failList;
    }

}
